//
//  MyInviteTableViewController.m
//  LPTClient
//
//  Created by apple on 2018/12/7.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "MyInviteTableViewController.h"
#import "LPTBaseRequest.h"
#import "MyInviteTableViewCell.h"

@interface MyInviteTableViewController ()

@property (nonatomic, strong) NSString *apiName;
@property (nonatomic, strong) NSMutableArray *mutableArr;
@property (nonatomic, assign) NSInteger currPage;

@end

@implementation MyInviteTableViewController


+ (instancetype)instance
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    MyInviteTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"MyInviteTableViewController"];
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [UIView new];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44;

    self.apiName = @"/api/invite_list";
    
    _mutableArr = [[NSMutableArray alloc] init];
    
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currPage = 1;
        [self requestList];
    }];
    
    self.tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.currPage ++;
        [self requestList];
    }];
    self.tableView.mj_footer = footer;

    [self.tableView.mj_header beginRefreshing];
//    _currPage = 1;
//    [self requestList];
}


- (void)requestList
{
    NSDictionary *body = @{@"page" : @(_currPage) , @"size" : @(30)};
    LPTBaseRequest  *requset = [[LPTBaseRequest alloc] initWithApi:self.apiName withBody:body withMethod:YTKRequestMethodGET];
    
    [requset startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];

        NSDictionary *model = (NSDictionary *)data;
        NSArray *array = model[@"invites"];
        if (self.currPage == 1) {
            [self.mutableArr removeAllObjects];
        }
        if (array.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.mutableArr addObjectsFromArray:array];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mutableArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyInviteTableViewCell *cell = (MyInviteTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyInviteTableViewCellID" forIndexPath:indexPath];
    
    [cell refreshWithDic:self.mutableArr[indexPath.row]];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
